#pragma once

#include "RBTree.h"
using my_rb_tree::RBTree;


namespace my_map
{
	template<class K, class V, class Compare = less<K>>
	class map
	{
		typedef K Key;
		typedef pair<const K, V> Value;

		struct KeyofValue //方便比较map和set复用代码比较key
		{
			const K& operator()(const Value& data)
			{
				return data.first;
			}
		};

	public:
		typedef typename RBTree<Key, Value, Compare, KeyofValue>::iterator iterator; //使用typename强调此是一个类型
		typedef typename RBTree<Key, Value, Compare, KeyofValue>::const_iterator const_iterator;
		typedef typename RBTree<Key, Value, Compare, KeyofValue>::reverse_iterator reverse_iterator;
		typedef typename RBTree<Key, Value, Compare, KeyofValue>::const_reverse_iterator const_reverse_iterator;

		iterator begin() {return _t.begin();}
		iterator end() { return _t.end(); }

		const_iterator begin() const { return _t.begin(); }
		const_iterator end() const { return _t.end(); }

		const_iterator cbegin() const { return _t.cbegin(); }
		const_iterator cend() const { return _t.cend(); }

		reverse_iterator rbegin(){ return _t.rbegin(); }
		reverse_iterator rend() { return _t.rend(); }

		const_reverse_iterator rbegin() const { return _t.rbegin(); }
		const_reverse_iterator rend() const { return _t.rend(); }

		const_reverse_iterator crbegin() const { return _t.crbegin(); }
		const_reverse_iterator crend() const { return _t.crend(); }

		pair<iterator, bool> insert(const Value& data)
		{
			return _t.Insert(data);
		}

		iterator find(const K& key)
		{
			return _t.Find(key);
		}

		V& operator[](const K& key)
		{
			return (((insert(make_pair(key, V()))).first)->second);

		}

		void swap(map<K, V, Compare>& m)
		{
			_t.swap(m._t);
		}

		map<K, V, Compare>& operator=(const map<K, V, Compare>& m)
		{
			_t = m._t;
			return *this;
		}

	private:
		RBTree<Key, Value, Compare, KeyofValue> _t;
	};
}

